package com.example.demo810.cfg;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author lzh
 * @date 2020/8/11 - 14:06
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {


    @Autowired
    ServerProperties serverProperties;


    private ApiInfo apiInfo() {
        String serviceUrl = "http://localhost:" + serverProperties.getPort() +
                serverProperties.getServlet().getContextPath() + "/index.html";
        return new ApiInfoBuilder()
                .title("权限管理系统后台接口文档")
                .description("WASD")
                .termsOfServiceUrl(serviceUrl)
                .version("1.0")
                .contact(new Contact("qaz", null, "1131201834@qq.com"))
                .build();
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("2.X版本接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo810.api"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * Knife4j可以配置多版本接口文档
     * https://doc.xiaominfo.com/guide/version-control.html
     *
     * @return
     */
    @Bean(value = "defaultApi3")
    public Docket defaultApi3() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("3.X版本接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo810.napi"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}
